# -*- coding: utf-8 -*-
import numpy as np
from gekko import GEKKO
import test_runner

def benchmark3():
    m = GEKKO()

    nt = 101
    m.time = np.linspace(0,1,nt)

    x1 = m.Var(value=1)
    x2 = m.Var(value=0)
    u = m.Var(lb=0, ub=5)

    final = np.zeros(nt)
    final[-1] = 1
    final = m.Param(value=final)


    m.Equation(x1.dt() == -(u+0.5*u**2)*x1)
    m.Equation(x2.dt() == u*x1)


    m.Obj(-final*x2)

    m.options.IMODE = 6

    m.solve(disp=False)
    assert x1.value == [1.0, 0.9897626, 0.9795456, 0.9693489, 0.9591726, 0.9490164, 0.9388805, 0.9287647, 0.918669, 0.9085933, 0.8985376, 0.8885018, 0.8784858, 0.8684897, 0.8585132, 0.8485564, 0.8386191, 0.8287014, 0.818803, 0.8089239, 0.7990641, 0.7892234, 0.7794017, 0.7695989, 0.7598149, 0.7500497, 0.740303, 0.7305747, 0.7208648, 0.711173, 0.7014993, 0.6918435, 0.6822053, 0.6725848, 0.6629816, 0.6533956, 0.6438265, 0.6342743, 0.6247387,0.6152194, 0.6057162, 0.5962288, 0.586757, 0.5773006, 0.5678592, 0.5584324, 0.54902, 0.5396217, 0.530237, 0.5208655, 0.5115069, 0.5021606, 0.4928263, 0.4835034, 0.4741914, 0.4648897, 0.4555977, 0.4463148, 0.4370403, 0.4277734, 0.4185133, 0.4092592, 0.4000102, 0.3907653, 0.3815233, 0.3722833, 0.3630438, 0.3538036, 0.3445612, 0.3353149, 0.3260631, 0.3168038, 0.3075349, 0.2982541, 0.2889588, 0.2796462, 0.2703132, 0.2609561, 0.2515712, 0.2421538, 0.232699, 0.223201, 0.2136533, 0.2040483, 0.1943772, 0.1846296, 0.1747934, 0.1648539, 0.1547935, 0.1445905, 0.1342179, 0.1236411, 0.1128152, 0.1016802, 0.09015248, 0.07811092, 0.06647803, 0.05657704, 0.04815068, 0.0409793, 0.034876]
    assert x2.value == [0.0, 0.007440616, 0.01485305, 0.02223722, 0.02959304, 0.03692043, 0.0442193, 0.05148956, 0.05873113, 0.06594391, 0.07312781, 0.08028274, 0.08740861, 0.09450531, 0.1015727, 0.1086108, 0.1156194, 0.1225984, 0.1295478, 0.1364673, 0.143357, 0.1502166, 0.1570461, 0.1638454, 0.1706143, 0.1773527, 0.1840605, 0.1907375, 0.1973837, 0.2039988, 0.2105828, 0.2171355, 0.2236568, 0.2301464, 0.2366044, 0.2430304, 0.2494244, 0.2557862, 0.2621156, 0.2684125, 0.2746766, 0.2809078, 0.2871059, 0.2932708, 0.2994021, 0.3054998, 0.3115635, 0.3175932, 0.3235885, 0.3295492, 0.3354752, 0.3413661, 0.3472217, 0.3530417, 0.3588259, 0.364574, 0.3702857, 0.3759607, 0.3815986, 0.3871993, 0.3927622, 0.3982871, 0.4037735, 0.4092212, 0.4146296, 0.4199984, 0.4253272, 0.4306153, 0.4358625, 0.441068, 0.4462315, 0.4513522, 0.4564297, 0.4614632, 0.4664519, 0.4713953, 0.4762924, 0.4811424, 0.4859444, 0.4906973, 0.4954001, 0.5000515, 0.5046501, 0.5091946, 0.5136832, 0.518114, 0.522485, 0.5267937, 0.5310373, 0.5352124, 0.539315, 0.5433401, 0.5472814, 0.5511306, 0.5548764, 0.5585023, 0.5618261, 0.5646549, 0.5670625, 0.5691114, 0.5708552]
test_runner.test('benchmark3', benchmark3)